package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryQueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //获取树状结构数据
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map> treeData(){
        return systemDictionaryService.queryTreeData();
    }


    @RequestMapping("/queryByParentId")
    @ResponseBody
    public List<SystemDictionary> queryByParentId(Long parentId){
        return systemDictionaryService.queryByParentId(parentId);
    }


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryQueryObject qo) {
        PageInfo<SystemDictionary> pageInfo = systemDictionaryService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //xxx
        //model.addAttribute("xxx", xxx);
        return "systemDictionary/list";
    }







    @RequestMapping("/delete")
    public String delete(Long id) {

        if (id != null) {
            systemDictionaryService.delete(id);
        }
        return "redirect:/systemDictionary/list"; // localhost/systemDictionary/list
    }

   /* @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionary systemDictionary) {

        if (systemDictionary.getId() == null) {
            systemDictionaryService.save(systemDictionary);
        } else {
            systemDictionaryService.update(systemDictionary);
        }
        return "redirect:/systemDictionary/list"; // localhost/systemDictionary/list
    }*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary) {

        if (systemDictionary.getId() == null) {
            systemDictionaryService.save(systemDictionary);
        } else {
            systemDictionaryService.update(systemDictionary);
        }
        return new JsonResult(true, "添加/更新成功");
    }

}
